# Python 2.6.4
# Project Euler, Problem 37
# Copyright 2010 Talha Zaman

from math import sqrt

def isprime(n):
    if n < lim: return n in primes
    i=0
    while primes[i]<=sqrt(n)+1:
        if n%primes[i]==0: return False
        i += 1
    return True

def trunc(n):
    if not isprime(n): return False
    sn = str(n)
    for i in range(1,len(sn)):
        if not isprime(int(sn[i:])): return False
        if not isprime(int(sn[:i])): return False
    return True   

def genprimes(p):
    if not isprime(p): return False
    if trunc(p) and p not in [2,3,5,7]: tpr.append(p)
    for i in [1,3,5,7,9]:
        genprimes(p*10+i)

lim = 100000
sieve = [0 for i in range(0, lim)]
sieve[0] = sieve[1] = 1
for i in range(2,lim):
    if sieve[i]: continue
    for j in range(2*i, lim, i):
        sieve[j] += 1
primes = [x for x in range(lim) if sieve[x]==0]
tpr = []
for i in [2,3,5,7]: genprimes(i)
print sum(tpr)
